package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.domain.source.repository.cache.OpenTransportCacheRepository;
import com.wlyuan.open.dto.transport.OpenTransportRelationSyncDTO;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherTransportRelationConversion implements OpenDispatcherConversion<OpenTransportRelationSyncDTO> {
    private final OpenTransportCacheRepository openTransportCacheRepository;


    @Override
    public void convert(OpenTransportRelationSyncDTO source) {
        //上游运单id
        var upTransport = source.getUpTransportId();
        if (LongUtils.notNullLong(upTransport)) {
            var upTransportId = openTransportCacheRepository.convert(upTransport);
            AssertUtils.assertNotNull(upTransportId, new ValueError("OpenTransportRelationSyncDTO 上游运单不存在:" + upTransport), source);
            source.setUpTransportId(upTransportId);
        }
        //下游运单id
        var downTransport = source.getDownTransportId();
        if (LongUtils.notNullLong(downTransport)) {
            var downTransportId = openTransportCacheRepository.convert(downTransport);
            AssertUtils.assertNotNull(downTransportId, new ValueError("OpenTransportRelationSyncDTO 下游运单不存在:" + downTransport), source);
            source.setDownTransportId(downTransportId);
        }
    }
}
